package club.justwill.single.persistence.dao;

import club.justwill.single.persistence.jpo.Orders;
import club.justwill.single.persistence.support.BaseDao;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.Query;
import java.util.List;

/**
 * Created by xiangnan on 10/28/15.
 */
@Repository
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public class OrderDao extends BaseDao<Orders> {

    public List<Orders> getById(long userId) {
        Query query = em.createQuery("select o from Orders o, OrderItem oi, Skus s where o.orderId = oi.orderId and oi.skuId = s.skusId and o.userId = :userId group by o.orderId");
        query.setParameter("userId",userId);
        return query.getResultList();
    }

    public Orders getByOrderId(long orderId) {
        Query query = em.createNamedQuery("order.getByOrderId").setParameter("orderId",orderId);
        try {
            return (Orders)query.getSingleResult();
        } catch (NonUniqueResultException ex) {
            return (Orders)query.getResultList().get(0);
        } catch (NoResultException e) {
            return null;
        }
    }

    public List<Orders> searchOrdersByProjectId(int index, int size, long projectId) {
        Query query = em.createQuery("select o from Orders o, OrderItem oi, Skus s, Projects p " +
                "where o.orderId = oi.orderId and oi.skuId = s.skusId and s.projectId = p.projectId and p.projectId = :projectId group by o.orderId order by o.processTimeStamp desc");
        query.setParameter("projectId",projectId)
                .setFirstResult(index)
                .setMaxResults(size);
        return query.getResultList();
    }
    public List<Orders> searchOrdersByProjectId(long projectId) {
        Query query = em.createQuery("select o from Orders o, OrderItem oi, Skus s, Projects p " +
                "where o.orderId = oi.orderId and oi.skuId = s.skusId and s.projectId = p.projectId and p.projectId = :projectId group by o.orderId");
        query.setParameter("projectId",projectId);
        return query.getResultList();
    }



    public List<Orders> getByUidAndPid(int index, int size, long userId, Long projectId) {

        Query query = em.createQuery("select o from Orders o, OrderItem oi, Skus s, Projects p " +
                "where o.orderId = oi.orderId and oi.skuId = s.skusId and s.projectId = p.projectId and o.userId = :userId and p.projectId = :projectId group by o.orderId");
        query.setParameter("userId", userId).setParameter("projectId", projectId)
                .setFirstResult(index)
                .setMaxResults(size);
        return query.getResultList();
    }


    public Orders getBySerialNumber(String serialNumber) {
        Query query = em.createQuery("select o from Orders o where o.serialNumber = :serialNumber");
        query.setParameter("serialNumber", serialNumber);
        try {
            return (Orders)query.getSingleResult();
        } catch (NonUniqueResultException ex) {
            return (Orders)query.getResultList().get(0);
        } catch (NoResultException e) {
            return null;
        }
    }

    public List<Orders> getgetNotPaidOrderByUserId(long userId) {
        Query query = em.createQuery("select o from Orders o where o.userId = :userId and o.status = 20");
        query.setParameter("userId",userId);
        return query.getResultList();
    }
}
